$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'device/device/list',
        datatype: "json",
        colModel: [
			/*{ label: '设备名称', name: 'deviceName', index: 'device_name', width: 80 },*/
			{ label: '设备ID', name: 'deviceId', index: 'device_id', width: 80 },
			{ label: 'IMEI', name: 'imei', index: 'imei', width: 80 }, 			
			{ label: '大楼编号', name: 'buildingCode', index: 'building_code', width: 80 }, 			
			{ label: '楼层', name: 'buildingFloor', index: 'building_floor', width: 80 }, 			
			{ label: '号', name: 'buildingRoom', index: 'building_room', width: 80 }, 			
			{ label: '状态', name: 'status', width: 60, formatter: function(value, options, row){
				return value === 0 ? 
					'<span class="label label-success">运行</span>' : 
					'<span class="label label-danger">解绑</span>';
			}},
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		device: {},
		buildingName:null,
		s1: {provinces:[],cities:[],countries:[],buildings:[],province:'',city:'',country:'',buildingName:''},
	},
	mounted:function(){
        this.s1getProvince();
    },
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.device = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.device.id == null ? "device/device/save" : "device/device/update";
			vm.device.buildingCode=vm.buildingName.split('-')[0];
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.device),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "device/device/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "device/device/info/"+id, function(r){
                vm.device = r.device;

				vm.s1.province=r.building.provinces
				vm.s1.city=r.building.cities
				vm.s1.town=r.building.counties
				vm.buildingName = r.device.buildingCode + "-" + r.device.buildingName;
				vm.s1getCity();
				vm.s1getCountry();
				vm.s1getBuilding();

				setTimeout(function () {
					vm.s1.province=r.building.provinces
					vm.s1.city=r.building.cities
					vm.s1.town=r.building.counties
					vm.buildingName = r.device.buildingCode + "-" + r.device.buildingName;
				},1000)

            });
		},
		
		s1getProvince: function () {
            var url = baseURL + "device/device/getBuildingArea?level=0";
            var _this=this;
            $.getJSON(url, function (data) {
                _this.s1.provinces = data.data;
            });
        },
        s1getCity: function () {
            var _this=this;
            var url = baseURL + "device/device/getBuildingArea?level=1&parentKey=" + vm.s1.province;
            $.getJSON(url, function (data) {
                _this.s1.cities = data.data;
            });
        },
        s1getCountry: function () {
            var _this=this;
            var url = baseURL + "device/device/getBuildingArea?level=2&parentKey=" + vm.s1.city;
            $.getJSON(url, function (data) {
                _this.s1.countries = data.data;
            });
        },
        s1getBuilding: function () {
            var _this=this;
            var url = baseURL + "device/device/getBuildingArea?level=3&parentKey=" + vm.s1.country;
            $.getJSON(url, function (data) {
                _this.s1.buildings = data.data;
            });
        },
		
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});